import json
def build_tree(data):
    new_data=json.loads(json.dumps(data))
    result = {}
    stack=[]
    for key,value in new_data.items():
        if "in" in value and len(value['in'].keys()):
            for v2 in new_data[key]['in'].values():
                out_dict=new_data[v2['in_id']]['out']
                print('匹配key和out_dict',key,out_dict)
                for k2 in out_dict:
                    out_id=out_dict[k2]['out_id']
                    if out_id==key:
                        out_dict[k2]["data"]=new_data[key]
                        break
            stack.append(key)
        else:
            result[key]=value
    return result

def get_config():
    return {
    "flow_chat_base_info": {
        "process_idx": "2",
        "process_dct": {
            "1": "mqtt进程",
            "2": "sqlite3进程"
        },
        "idx": "5",
        "flow_chat_name": "额"
    },
    "flow_chat_use_compontent": {
        "1": {
            "name": "mqtt初始化",
            "note": "会创建进程,进程内连接mqtt服务器,然后监听topic进行处理。",
            "temp_type": "process",
            "tree_type": 0,
            "process_name": "mqtt进程",
            "in_kwargs": {
                "0": {
                    "name": "mqtt_address",
                    "note": "mqtt地址",
                    "default": "127.0.0.1",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "127.0.0.1",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "1": {
                    "name": "mqtt_port",
                    "note": "mqtt端口",
                    "default": 1883,
                    "type": "int",
                    "in_type1": "0",
                    "v0": 1883,
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "2": {
                    "name": "mqtt_username",
                    "note": "mqtt登录用户名",
                    "default": "default",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "default",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "3": {
                    "name": "mqtt_passwd",
                    "note": "mqtt登录密码",
                    "default": "default",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "default",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "4": {
                    "name": "mqtt_client_name",
                    "note": "mqtt注册名称",
                    "default": "client_name",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "client_name",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "5": {
                    "name": "recv_port",
                    "note": "进程接收其他进程消息的socket服务端口",
                    "default": 5555,
                    "type": "int",
                    "in_type1": "0",
                    "v0": 5555,
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "6": {
                    "name": "recv_ip",
                    "note": "进程接收其他进程消息的socket服务ip",
                    "default": "127.0.0.1",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "127.0.0.1",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "-1": {
                    "name": "flag",
                    "note": "通过传入值来控制组件的运行顺序 不传值时表示按照默认方式运行",
                    "default": "0",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "0",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                }
            },
            "out_kwargs": {
                "0": {
                    "name": "进程通信端口",
                    "note": "mqtt进程的recv_port",
                    "type": "int"
                },
                "1": {
                    "name": "mqtt对象",
                    "note": "mqtt进程的recv_port",
                    "type": "int",
                    "out_params": {
                        "2": {
                            "0": {
                                "key": [
                                    "1",
                                    "1"
                                ]
                            }
                        },
                        "3": {
                            "0": {
                                "key": [
                                    "3",
                                    "3"
                                ]
                            }
                        }
                    }
                }
            },
            "tree_type_from": {},
            "process_id": "1",
            "process_from": {
                "1": {
                    "1": [
                        "1"
                    ],
                    "3": [
                        "1",
                        "2",
                        "5",
                        "3",
                        "1"
                    ]
                }
            },
            "process_change": True,
            "temp_name": "mqtt初始化",
            "x": 177.7666473388672,
            "y": 85.06665802001953,
            "out_idx": "3",
            "in_idx": 0,
            "out": {
                "1": {
                    "out_id": "2",
                    "out_key": "1"
                },
                "3": {
                    "out_id": "3",
                    "out_key": "3"
                }
            },
            "in": {}
        },
        "2": {
            "name": "收mqtt数据",
            "note": "监听topic然后将获取到的topic_lst和data输出,仅支持绝对topic和/#语法",
            "id": "基础组件_mqtt通信_收mqtt数据",
            "index": 0,
            "tree_type": 1,
            "in_kwargs": {
                "0": {
                    "name": "mqtt对象",
                    "note": "mqtt初始化后传出的对象",
                    "default": "",
                    "type": "int",
                    "in_type1": "1",
                    "v0": "",
                    "v1_": [
                        "1",
                        "1"
                    ],
                    "v2": "",
                    "in_type3": "0",
                    "in_params": [
                        "1",
                        "1",
                        [
                            "1",
                            "1"
                        ]
                    ]
                },
                "1": {
                    "name": "收topic",
                    "note": "只支持绝对匹配以及/#匹配,匹配速度100w条小于0秒",
                    "default": "test/#",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "test/#",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "-1": {
                    "name": "flag",
                    "note": "通过传入值来控制组件的运行顺序 不传值时表示按照默认方式运行",
                    "default": "0",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "0",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                }
            },
            "out_kwargs": {
                "0": {
                    "name": "topic_lst",
                    "note": "topic列表以/分割",
                    "type": "list"
                },
                "1": {
                    "name": "data",
                    "note": "topic原始数据",
                    "type": "str",
                    "out_params": {
                        "5": {
                            "1": {
                                "key": [
                                    "1",
                                    "1"
                                ]
                            }
                        }
                    }
                }
            },
            "tree_type_from": {},
            "process_id": "1",
            "process_from": {
                "1": {
                    "1": [
                        "1",
                        "2"
                    ]
                }
            },
            "temp_name": "收mqtt数据",
            "x": 258.7666473388672,
            "y": 187.06665802001953,
            "out_idx": "1",
            "in_idx": "1",
            "out": {
                "1": {
                    "out_id": "5",
                    "out_key": "1"
                }
            },
            "in": {
                "1": {
                    "in_id": "1",
                    "in_key": "1"
                }
            }
        },
        "3": {
            "name": "发mqtt数据",
            "id": "基础组件_mqtt通信_发mqtt数据",
            "index": 0,
            "tree_type": "1",
            "in_kwargs": {
                "0": {
                    "name": "mqtt对象",
                    "note": "mqtt初始化后传出的对象",
                    "default": "",
                    "type": "int",
                    "in_type1": "1",
                    "v0": "",
                    "v1_": [
                        "1",
                        "1"
                    ],
                    "v2": "",
                    "in_type3": "0",
                    "in_params": [
                        "1",
                        "1",
                        [
                            "3",
                            "3"
                        ]
                    ]
                },
                "1": {
                    "name": "topic",
                    "note": "要发送的topic",
                    "default": "/wewe",
                    "type": "int",
                    "in_type1": "0",
                    "v0": "/wewe",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "2": {
                    "name": "info",
                    "note": "操作数据",
                    "default": "127.0.0.1",
                    "type": "dict",
                    "in_type1": "1",
                    "v0": "127.0.0.1",
                    "v1_": [
                        "5",
                        "0"
                    ],
                    "v2": "",
                    "in_type3": "0",
                    "in_params": [
                        "5",
                        "0",
                        [
                            "1",
                            "1"
                        ]
                    ]
                },
                "-1": {
                    "name": "flag",
                    "note": "通过传入值来控制组件的运行顺序 不传值时表示按照默认方式运行",
                    "default": "0",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "0",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                }
            },
            "out_kwargs": {
                "0": {
                    "name": "输出数据",
                    "note": "输出数据"
                }
            },
            "tree_type_from": {
                "5": "1"
            },
            "process_id": "1",
            "process_from": {
                "1": {
                    "1": [
                        "1",
                        "3"
                    ],
                    "5": [
                        "1",
                        "2",
                        "5",
                        "3"
                    ]
                }
            },
            "temp_name": "发mqtt数据",
            "x": 263.7666473388672,
            "y": 499.06665802001953,
            "out_idx": 0,
            "in_idx": "3",
            "out": {},
            "in": {
                "1": {
                    "in_id": "5",
                    "in_key": "1"
                },
                "3": {
                    "in_id": "1",
                    "in_key": "3"
                }
            }
        },
        "4": {
            "name": "数据库初始化",
            "id": "基础组件_数据库操作_数据库初始化",
            "index": 0,
            "tree_type": 0,
            "temp_type": "process",
            "process_name": "sqlite3进程",
            "in_kwargs": {
                "0": {
                    "name": "recv_port",
                    "note": "进程接收其他进程消息的socket服务端口",
                    "default": 5555,
                    "type": "int",
                    "in_type1": "0",
                    "v0": 5555,
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "1": {
                    "name": "recv_ip",
                    "note": "进程接收其他进程消息的socket服务ip",
                    "default": "127.0.0.1",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "127.0.0.1",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                },
                "-1": {
                    "name": "flag",
                    "note": "通过传入值来控制组件的运行顺序 不传值时表示按照默认方式运行",
                    "default": "0",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "0",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                }
            },
            "out_kwargs": {
                "0": {
                    "name": "db对象",
                    "note": "db进程的recv_port",
                    "type": "int",
                    "out_params": {
                        "5": {
                            "0": {
                                "key": [
                                    "1",
                                    "2"
                                ]
                            }
                        }
                    }
                },
                "1": {
                    "name": "进程通信端口",
                    "note": "db进程的recv_port",
                    "type": "int"
                }
            },
            "tree_type_from": {},
            "process_id": "2",
            "process_from": {
                "1": {
                    "5": [
                        "1",
                        "2",
                        "5",
                        "4"
                    ]
                },
                "2": {
                    "4": [
                        "4"
                    ]
                }
            },
            "process_change": True,
            "temp_name": "数据库初始化",
            "x": 527.7666473388672,
            "y": 82.06665802001953,
            "out_idx": "1",
            "in_idx": 0,
            "out": {
                "1": {
                    "out_id": "5",
                    "out_key": "2"
                }
            },
            "in": {}
        },
        "5": {
            "name": "写数据库",
            "id": "基础组件_数据库操作_写数据库",
            "index": 0,
            "tree_type": "1",
            "in_kwargs": {
                "0": {
                    "name": "db对象",
                    "note": "进程接收其他进程消息的socket服务端口",
                    "default": 5555,
                    "type": "int",
                    "in_type1": "1",
                    "v0": 5555,
                    "v1_": [
                        "4",
                        "0"
                    ],
                    "v2": "",
                    "in_type3": "0",
                    "in_params": [
                        "4",
                        "0",
                        [
                            "1",
                            "2"
                        ]
                    ]
                },
                "1": {
                    "name": "info",
                    "note": "操作数据",
                    "default": "127.0.0.1",
                    "type": "dict",
                    "in_type1": "1",
                    "v0": "127.0.0.1",
                    "v1_": [
                        "2",
                        "1"
                    ],
                    "v2": "",
                    "in_type3": "0",
                    "in_params": [
                        "2",
                        "1",
                        [
                            "1",
                            "1"
                        ]
                    ]
                },
                "-1": {
                    "name": "flag",
                    "note": "通过传入值来控制组件的运行顺序 不传值时表示按照默认方式运行",
                    "default": "0",
                    "type": "str",
                    "in_type1": "0",
                    "v0": "0",
                    "v1_": [
                        None,
                        None
                    ],
                    "v2": "",
                    "in_type3": "0"
                }
            },
            "out_kwargs": {
                "0": {
                    "name": "输出数据",
                    "note": "输出数据",
                    "out_params": {
                        "3": {
                            "2": {
                                "key": [
                                    "1",
                                    "1"
                                ]
                            }
                        }
                    }
                }
            },
            "tree_type_from": {
                "2": "1"
            },
            "process_id": "1",
            "process_from": {
                "1": {
                    "2": [
                        "1",
                        "2",
                        "5"
                    ]
                },
                "2": {
                    "4": [
                        "4",
                        "5"
                    ]
                }
            },
            "temp_name": "写数据库",
            "x": 424.7666473388672,
            "y": 275.06665802001953,
            "out_idx": "1",
            "in_idx": "2",
            "out": {
                "1": {
                    "out_id": "3",
                    "out_key": "1"
                }
            },
            "in": {
                "1": {
                    "in_id": "2",
                    "in_key": "1"
                },
                "2": {
                    "in_id": "4",
                    "in_key": "1"
                }
            }
        }
    }
}

if __name__=="__main__":
    test_d=get_config()['flow_chat_use_compontent']
    rv=build_tree(test_d)
    import json,os
        # 写入JSON文件  
    with open(os.path.dirname(os.path.abspath(__file__))+os.sep+'流程字典转树结构流程字典.json', 'w', encoding='utf-8') as f:  
        json.dump(build_tree(test_d), f, ensure_ascii=False, indent=4)

    # with
    # print()